﻿#region Header

/*
Behavioral Rating of Dancing Human Crowds based on Motion Patterns
By

Pascal Hauser 
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

and

Raphael Gfeller
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

*/

#endregion

#region Usings

using System;
using System.IO;
using Emgu.CV;
using Emgu.CV.Structure;
using paravili.Services;
using Sebarf.Services.Interfaces;

#endregion

namespace paravili.Steps {
	/// <summary>
	/// stores the image hold by the <see cref="IImageLocatorService"/> with the id given by the ImageProviderId to the hard disk
	/// </summary>
	public class StoreImageToDisk : ProcessStepWithMeasurement<Object> {
		#region Public Properties

		[ConfigurabelStringValue(Name = "OutputPath")]
		public string OutputPath { get; set; }

		[ServiceRequest]
		public IImageLocatorService ImageLocatorService { get; set; }

		[ConfigurabelStringValue(Name = "ImageProviderId")]
		public String ImageProviderId { get; set; }

		[ConfigurabelBooleanValue(Name = "IsEnable")]
		public bool IsEnable { get; set; }

		[ServiceRequest]
		public IResetHandlerService ResetHandlerService {
			set { value.Reset += OnReset; }
		}

		#endregion

		#region Public Methods

		public StoreImageToDisk() {
			OutputPath = Directory.GetCurrentDirectory();
			ImageProviderId = "NextImageProvider.ImageData";
		}

		protected override object OnProcess(object toProcess) {
			if (IsEnable) {
				Image<Lab, byte> img = ImageLocatorService.GetImage(ImageProviderId);
				if (img != null) {
					img.Save(OutputPath + "\\" + (m_index++).ToString("00000\\.jpg"));
				}
			}
			return toProcess;
		}

		private void OnReset(object sender, EventArgs e) {
			m_index = 0;
		}

		#endregion

		#region Private Fields

		private int m_index;

		#endregion
	}
}